package com.ruoyi.business.controller;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.DispatchLists;
import com.ruoyi.business.service.IDispatchListsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.business.dto.DispatchListsExportDTO;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * DispatchListsController
 *
 * @author liutietou
 * @since 1.0 2025-07-19
 */
@RestController
@RequestMapping("/business/DispatchLists")
public class DispatchListsController extends BaseController {
    @Autowired
    private IDispatchListsService dispatchListsService;

    /**
     * 查询DispatchLists列表
     */
   // @PreAuthorize("@ss.hasPermi('business:DispatchLists:list')")
    @GetMapping("/list")
    public TableDataInfo list(DispatchLists dispatchLists) {
        Page<DispatchLists> page = dispatchListsService.findDispatchListsPage(dispatchLists);
        return getDataTable(page);
    }

    /**
     * 导出DispatchLists列表
     */
   // @PreAuthorize("@ss.hasPermi('business:DispatchLists:export')")
    @Log(title = "DispatchLists", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(DispatchLists dispatchLists) {
        List<DispatchLists> list = dispatchListsService.findDispatchListsList(dispatchLists);
        ExcelUtil<DispatchLists> util = new ExcelUtil<>(DispatchLists.class);
        return util.exportExcel(list, "DispatchLists");
    }

    /**
     * 获取DispatchLists详细信息
     */
  //  @PreAuthorize("@ss.hasPermi('business:DispatchLists:query')")
    @GetMapping(value = "/{autoid}")
    public AjaxResult getInfo(@PathVariable("autoid") Integer autoid) {
        return AjaxResult.success(dispatchListsService.findById(autoid));
    }

    /**
     * 新增DispatchLists
     */
 //   @PreAuthorize("@ss.hasPermi('business:DispatchLists:add')")
    @Log(title = "DispatchLists", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DispatchLists dispatchLists) {
        dispatchListsService.save(dispatchLists);
        return AjaxResult.success();
    }

    /**
     * 修改DispatchLists
     */
  //  @PreAuthorize("@ss.hasPermi('business:DispatchLists:edit')")
    @Log(title = "DispatchLists", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DispatchLists dispatchLists) {
        dispatchListsService.save(dispatchLists);
        return AjaxResult.success();
    }

    /**
     * 删除DispatchLists
     */
 //   @PreAuthorize("@ss.hasPermi('business:DispatchLists:remove')")
    @Log(title = "DispatchLists", businessType = BusinessType.DELETE)
	@DeleteMapping("/{autoids}")
    public AjaxResult remove(@PathVariable Integer[] autoids) {
        dispatchListsService.deleteByIds(Arrays.asList(autoids));
        return AjaxResult.success();
    }

    /**
     * 导出PDF
     */
 //   @PreAuthorize("@ss.hasPermi('business:DispatchLists:export')")
    @Log(title = "DispatchLists PDF导出", businessType = BusinessType.EXPORT)
    @PostMapping("/exportPDF")
    public void exportPDF(@RequestBody DispatchListsExportDTO exportDTO, HttpServletResponse response) {
        try {
            // 设置响应头
            response.setContentType("application/pdf");
            response.setHeader("Content-Disposition", "attachment; filename=调度单明细表.pdf");
            
            // 调用服务生成PDF
            dispatchListsService.exportPDF(exportDTO, response.getOutputStream());
            
        } catch (Exception e) {
            logger.error("PDF导出失败", e);
            throw new RuntimeException("PDF导出失败：" + e.getMessage());
        }
    }
}
